using System;
using System.Linq;
using System.Web.Mvc;
using RestUpMVC.BlogService.Resources;

namespace RestUpMVC.BlogService.Controllers
{
    public class PostController : Controller
    {
        public ActionResult List(Guid blogId, int? start, int? limit)
        {
            if (!start.HasValue)
            {
                start = 0;
            }
            if (!limit.HasValue)
            {
                limit = 25;
            }

            var blog = Domain.Blog.Get(blogId);
            if (null == blog)
            {
                return this.RestNotFound(new { Message = "The resource was not found." });
            }

            var posts = Domain.Post.GetByBlog(blog, (int) start, (int) limit);
            if (0 == posts.Items.Count())
            {
                return this.RestNoContent();
            }

            return this.RestOk(posts);
        }

        public ActionResult Get(Guid blogId, Guid id)
        {
            throw new NotImplementedException();
        }

        public ActionResult Post(Guid blogId, Post post)
        {
            var blog = Domain.Blog.Get(blogId);
            if (null==blog)
            {
                return this.RestNotFound(new { Message = "The resource was not found." });
            }
            var domain = new Domain.Post {Content = post.Content};
            Domain.Post.Create(blog, ref domain);
            post.Id = domain.Id;
            return this.RestOk(post);
        }

        public ActionResult Put(Guid blogId, Guid id, Post post)
        {
            throw new NotImplementedException();
        }

        public ActionResult Delete(Guid blogId, Guid id)
        {
            throw new NotImplementedException();
        }
    }
}